package com.waimai.mobile.ui.activity;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.waimai.mobile.R;
import com.waimai.mobile.api.WaiMaiServer;
import com.waimai.mobile.api.entity.ReceiveAddress;
import com.waimai.mobile.api.model.ResponseModel;
import com.waimai.mobile.ui.WaiMaiApp;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;

/**
 * Created by lin on 2015/6/22.
 */
@EActivity(R.layout.activity_address)
public class AddressActivity extends BaseActivity02{

    @App
    public WaiMaiApp app;

    @RestService
    public WaiMaiServer server;

    @Extra("address")
    public ReceiveAddress addressExta = null;

    @ViewById
    public EditText userNameEdt;

    @ViewById
    public EditText phoneEdt;

    @ViewById
    public EditText addressNameEdt;

    @AfterViews
    public void afterViews(){
        if(addressExta == null){
            setActionBarTitle("新增收货地址");
            userNameEdt.setText("");
            phoneEdt.setText("");
            addressNameEdt.setText("");
        }else{
            setActionBarTitle("编辑收货地址");
            userNameEdt.setText(addressExta.getUserName());
            phoneEdt.setText(addressExta.getPhone());
            addressNameEdt.setText(addressExta.getAddressName());
        }
    }


    @Click(R.id.handleAddressBtn)
    public void handdleAddressBtnClick(View v){
        String userName = userNameEdt.getText().toString().trim();
        String phone = phoneEdt.getText().toString().trim();
        String addressName = addressNameEdt.getText().toString().trim();

        if(phone == null || "".equals(phone)){
            Toast.makeText(this, "请填写手机号码", Toast.LENGTH_SHORT).show();
            return ;
        }
        if(addressName == null || "".equals(addressName)){
            Toast.makeText(this, "请填写收货地址", Toast.LENGTH_SHORT).show();
            return ;
        }


        if(addressExta == null){
            addressExta = new ReceiveAddress(addressName, phone);
            addressExta.setUser(app.getUser());
            if(userName != null && !"".equals(userName)){
                addressExta.setUserName(userName);
            }
            addAddress(addressExta);
        }else{
            if(userName != null && !"".equals(userName)){
                addressExta.setUserName(userName);
            }else{
                addressExta.setAddressName(null);
            }
            addressExta.setPhone(phone);
            addressExta.setUserName(userName);
            addressExta.setUser(app.getUser());
            updateAddress(addressExta);
        }
    }

    @Background
    public void addAddress(ReceiveAddress address){
        try{
            ResponseModel<ReceiveAddress> model = server.address(app.getUser().getUserId(), address);
            if(model != null){
                switch (model.getStatus().getStatusCode()){
                    case ResponseModel.STATUS_REQUEST_SUCCESS:
                        reqeustSuccess();
                        break;
                    case ResponseModel.STATUS_REQUEST_FAILED:
                        reqeustFailed();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                }
            }else{
                serverError();
            }

        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @Background
    public void updateAddress(ReceiveAddress address){
        try{
            ResponseModel<ReceiveAddress> model = server.address(app.getUser().getUserId(), address.getAddressId(), address);
            if(model != null){
                switch (model.getStatus().getStatusCode()){
                    case ResponseModel.STATUS_REQUEST_SUCCESS:
                        reqeustSuccess();
                        break;
                    case ResponseModel.STATUS_REQUEST_FAILED:
                        reqeustFailed();
                        break;
                    case ResponseModel.STATUS_SERVER_ERROR:
                        serverError();
                }
            }else{
                serverError();
            }

        }catch (Exception e){
            e.printStackTrace();
            netError();
        }
    }

    @UiThread
    public void reqeustSuccess(){
        Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show();
        onBackPressed();
    }

    @UiThread
    public void reqeustFailed(){
        Toast.makeText(this, "请求失败，请重试", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void serverError(){
        Toast.makeText(this, "服务器错误，请重试", Toast.LENGTH_SHORT).show();
    }

    @UiThread
    public void netError(){
        Toast.makeText(this, "网络错误，请重试", Toast.LENGTH_SHORT).show();
    }

}
